package com.huobao.yunying.fragments;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.zhangying.R;
import com.huobao.yunying.activitys_store.Act_Store;
import com.huobao.yunying.adapters.Adapter_Dailipin;
import com.huobao.yunying.entity.FristPageList;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment_DailiPin extends BaseFragment implements OnClickListener, OnScrollListener, OnItemClickListener {

	private Http m_Http;
	private Adapter_Dailipin m_Adapter;
	private ListView m_ListView;
	private String cat_id;
	private ImageView btn_clicktotop;
	private int m_CurrentPage = 1;
	private View footView;
	private TextView footMsg;
	private boolean isLoading = false;

	public static Fragment_DailiPin getInstant(String id){
		Fragment_DailiPin instance = new Fragment_DailiPin();
		Bundle args = new Bundle();
		args.putString("id", id);
		instance.setArguments(args);
		return instance;
	}

	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_listview, null);
		this.cat_id = getArguments().getString("id");
		m_ListView = (ListView) view.findViewById(R.id.listview);
		footView = inflater.inflate(R.layout.item_footview, null);
		footMsg = (TextView) footView.findViewById(R.id.tv_message);
		m_ListView.addFooterView(footView);
		btn_clicktotop = (ImageView) view.findViewById(R.id.btn_totop);
		btn_clicktotop.setOnClickListener(this);
		m_Adapter = new Adapter_Dailipin(getActivity());
		m_ListView.setAdapter(m_Adapter);
		m_ListView.setOnItemClickListener(this);
		m_ListView.setOnScrollListener(this);
		m_Http = Http.getInstant();
		getData(false);
		return view;
	}

	private void getData(boolean showDialog) {
		isLoading = true;
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, HttpConst.dlp, HttpConst.SECOND_KEY,
				HttpConst.dlp_list, "cateid", cat_id, "page", m_CurrentPage + "");
		m_Http.sendRequest(getActivity(), showDialog, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<FristPageList> list = JSON.parseArray(jsonObject.getString("retval"), FristPageList.class);

				if (list == null || list.size() == 0) {
					m_ListView.removeFooterView(footView);
					isLoading = true;
					return;
				}
				m_Adapter.addAll(list);
				isLoading = false;
				showContentView();
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_totop:
			m_ListView.smoothScrollToPosition(0);
			break;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (firstVisibleItem >= 1) {
			btn_clicktotop.setVisibility(View.VISIBLE);
		} else {
			btn_clicktotop.setVisibility(View.GONE);
		}

		if ((firstVisibleItem + visibleItemCount) >= totalItemCount) {
			if (isLoading) {
				return;
			}
			isLoading = true;
			m_CurrentPage++;
			getData(false);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent(getActivity(), Act_Store.class);
		intent.putExtra("id", m_Adapter.getItem(position).getStore_id());
		getActivity().startActivity(intent);
	}
}
